package io.renren.modules.statistis.controller;

import com.alibaba.fastjson.JSONArray;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.generator.entity.TObjectEntity;
import io.renren.modules.generator.entity.TTrainingCheckEntity;
import io.renren.modules.generator.entity.TTrainingScoreEntity;
import io.renren.modules.generator.service.TTrainingCheckService;
import io.renren.modules.statistis.service.statistisService;
import io.renren.modules.statistis.statistisEntity;
import io.renren.modules.user.entity.TPeopleEntity;
import io.renren.modules.user.entity.TPeopleFireDetachmentEntity;
import io.renren.modules.user.entity.vo.PeopleVo;
import io.renren.modules.user.entity.vo.PeopleZZVo;
import io.swagger.models.auth.In;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/statistis")
public class statistisController {

    @Autowired
    private statistisService statiService;
    @Autowired
    private TTrainingCheckService tTrainingCheckService;

    /**
     * 测试接口
     */
    @RequestMapping("/getTest")
    //@RequiresPermissions("generator:ttraining:list")
    public R getTest(){
        List<TObjectEntity> page=statiService.getTest();
        return R.ok().put("page",page);
    }

    /**
     * 按人员类型进行分类统计
     */
    @RequestMapping("/getTPeople")
    //@RequiresPermissions("generator:ttraining:list")
    public R getTPeople(@RequestBody statistisEntity read){
        List<TPeopleEntity> page=statiService.getTPeople(read);
        return R.ok().put("page",page);
    }
    /**
     *按下级部门进行分类统计
     */
    @RequestMapping("/getTPeopleFireDetachment")
    //@RequiresPermissions("generator:statistis:list")
    public R getTPeopleFireDetachment(@RequestBody statistisEntity read){
        List<TPeopleFireDetachmentEntity> page=statiService.getTPeopleFireDetachment(read);
        return R.ok().put("page",page);
    }
    /**
     * 人员自定义分析
     */
    @RequestMapping("/getSelfEmployed")
    //@RequiresPermissions("generator:statistis:list")
    public R getSelfEmployed(@RequestBody statistisEntity read){
        List<TPeopleEntity> page=statiService.getSelfEmployed(read);
        return R.ok().put("page",page);
    }
    /**
     * 人员衔级分布
     */
    @RequestMapping("/getRyxjfb")
    //@RequiresPermissions("generator:statistis:list")
    public R getRyxjfb(@RequestBody PeopleVo peopleVo){
        List<Map<String,Object>> page=statiService.getRyxjfb(peopleVo);
        return R.ok().put("page",page);
    }


    /**
     * A．战斗类型统计
     */
    @RequestMapping("/getZdlxtj")
    //@RequiresPermissions("generator:statistis:list")
    public R getZdlxtj(@RequestBody statistisEntity read){
        List<Map<String,Object>> page=statiService.getZdlxtj(read);
        return R.ok().put("page",page);
    }

    /**
     *B．下属单位战斗次数统计
     */
    @RequestMapping("/getXsdwzdcstj")
    //@RequiresPermissions("generator:statistis:list")
    public R getXsdwzdcstj(@RequestBody statistisEntity read){
        List<Map<String,Object>> page=statiService.getXsdwzdcstj(read);
        return R.ok().put("page",page);
    }

    /**
     * C．本年度战斗次数统计
     */
    @RequestMapping("/getBndzdcstj")
    //@RequiresPermissions("generator:statistis:list")
    public R getBndzdcstj(@RequestBody statistisEntity read){
        List<Map<String,Object>> page=statiService.getBndzdcstj(read);
        return R.ok().put("page",page);
    }

    /**
     * D．历史年份同月战斗次数类型统计
     */
    @RequestMapping("/getLsnftyzdcslxtj")
    //@RequiresPermissions("generator:statistis:list")
    public R getLsnftyzdcslxtj(@RequestBody statistisEntity read){
        List<Map<String,Object>> page=statiService.getLsnftyzdcslxtj(read);
        return R.ok().put("page",page);
    }

    /*
     * 人员分布图（专职消防员）
     * */
    @RequestMapping("/getRyhtfb")
    public R findPeopleZZList(@RequestBody PeopleVo parentId){
        List<PeopleZZVo> peopleList = statiService.getRyhtfb(parentId.getDeptId(),parentId.getSquadron());
        return R.ok().put("data",peopleList);
    }
    /**
     * 考核成绩 分布图
     */
    @RequestMapping("/achievementStatistics")
    //@RequiresPermissions("generator:statistis:list")
    public R achievementStatistics(@RequestBody statistisEntity read){
        List<TTrainingScoreEntity> page=statiService.achievementStatistics(read);
        return R.ok().put("page",page);
    }
    /**
     * 下级单位考核合格率分析图
     */
    @RequestMapping("/passRateStatistics")
    //@RequiresPermissions("generator:statistis:list")
    public R passRateStatistics(@RequestBody statistisEntity read){
        List<TTrainingScoreEntity> page=statiService.passRateStatistics(read);
        return R.ok().put("page",page);
    }

    /**
     * #单位考核均值
     */
    @RequestMapping("/companyStatistics")
    //@RequiresPermissions("generator:statistis:list")
    public R companyStatistics(@RequestBody statistisEntity read){
        List<TTrainingScoreEntity> page=statiService.companyStatistics(read);
        return R.ok().put("page",page);
    }
    /**
     * 历史成绩曲线图
     */
    @RequestMapping("/historicalResultsStatistics")
    //@RequiresPermissions("generator:statistis:list")
    public R historicalResultsStatistics(@RequestBody statistisEntity read){
        List<TTrainingScoreEntity> page=statiService.historicalResultsStatistics(read);
        return R.ok().put("page",page);
    }
    /**
     * 自主 人员分布
     */
    @RequestMapping("/peopleStatistics")
    //@RequiresPermissions("generator:statistis:list")
    public R peopleStatistics(@RequestBody statistisEntity read){
        List<TPeopleEntity> page=statiService.peopleStatistics(read);
        return R.ok().put("page",page);
    }
    /**
     * 人员参训情况统计分析
     */
    @RequestMapping("/peopleJoinStatistics")
    //@RequiresPermissions("generator:statistis:list")
    public R peopleJoinStatistics(@RequestBody statistisEntity read){
        List<TPeopleEntity> page=statiService.peopleJoinStatistics(read);
        return R.ok().put("page",page);
    }
    /**
     * 人员带训情况
     */
    @RequestMapping("/peopleUncommittedStatistics")
    //@RequiresPermissions("generator:statistis:list")
    public R peopleUncommittedStatistics(@RequestBody statistisEntity read){
        List<TPeopleEntity> page=statiService.peopleUncommittedStatistics(read);
        return R.ok().put("page",page);
    }
    /**
     * 人员战斗情况分析
     */
    @RequestMapping("/fightPeopleStatistics")
    //@RequiresPermissions("generator:statistis:list")
    public R fightPeopleStatistics(@RequestBody statistisEntity read){
        List<TPeopleEntity> page=statiService.fightPeopleStatistics(read);
        return R.ok().put("page",page);
    }

    /**
     * 考核成绩分布
     */
    @RequestMapping("/findScoreDistribution")
    //@RequiresPermissions("generator:statistis:list")
    public R findScoreDistribution(Integer trainingId,Integer checkCount){
        if(trainingId==null || checkCount==null){
            return R.error("训练ID和训练次数不能为空");
        }
        TTrainingCheckEntity tTrainingCheckEntity=tTrainingCheckService.selectByTrainingIdAndCount(trainingId,checkCount);
        List<Map<String, Object>> page=null;
        if(tTrainingCheckEntity!=null){
            page=statiService.findScoreDistribution(tTrainingCheckEntity.getId());
        }
        return R.ok().put("page",page);
    }

    /**
     * 考核成绩分布
     */
    @RequestMapping("/findPassRate")
    //@RequiresPermissions("generator:statistis:list")
    public R findScoreDistribution(Integer trainingCheckId,Integer type,Integer parentId){
        if(trainingCheckId==null || type==null || parentId==null){
            return R.error("参数不能为空");
        }
        List<Map<String, Object>> page=null;
        if(type==1){
            page=statiService.findDeptPassRate(trainingCheckId);
        }else if(type==2){
            page=statiService.findZdeptPassRate(trainingCheckId,parentId);
        }else if(type==3){
            page=statiService.findClassPassRate(trainingCheckId,parentId);
        }
        return R.ok().put("page",page);
    }

    /**
     * 重大考核合格率走势
     */
    @RequestMapping("/findPassRateTrend")
    public R findScoreDistribution(Integer trainingId){
        if(trainingId==null ){
            return R.error("参数不能为空");
        }
        List<Map<String, Object>> page=statiService.findImportantTrainingPassTrend(trainingId);
        return R.ok().put("page",page);
    }

    /**
     * 历史成绩曲线图
     */
    @RequestMapping("/peopleHistoryScore")
    public R peopleHistoryScore(Integer peopleId){
        if(peopleId==null ){
            return R.error("参数不能为空");
        }
        List<Map<String, Object>> page=statiService.peopleHistoryScore(peopleId);
        return R.ok().put("page",page);
    }

    /**
     * 能力分析雷达图
     */
    @RequestMapping("/peopleAbilityAnalysis")
    public R peopleAbilityAnalysis(Integer peopleId){
        if(peopleId==null ){
            return R.error("参数不能为空");
        }
        List<Map<String, Object>> page=statiService.peopleAbilityAnalysis(peopleId);
        return R.ok().put("page",page);
    }

    /**
     * 个人考核指标成长曲线图
     */
    @RequestMapping("/peoplePersonalGrowth")
    public R peoplePersonalGrowth(Integer peopleId,String objectIdList){
        if(peopleId==null|| objectIdList==null){
            return R.error("参数不能为空");
        }
        List<Integer> objectList = parseStrToList(objectIdList);
        List<Map<String, Object>> page=statiService.peoplePersonalGrowth(peopleId,objectList);
        return R.ok().put("page",page);
    }

    /**
     * 人员成绩对比
     */
    @RequestMapping("/personalScoreCompare")
    public R personalScoreCompare(Integer trainingCheckId,String peopleIdList){
        if(trainingCheckId==null ||peopleIdList==null ){
            return R.error("参数不能为空");
        }
        List<Integer> peopleList=parseStrToList(peopleIdList);
        List<Map<String, Object>> page=statiService.personalScoreCompare(trainingCheckId,peopleList);
        return R.ok().put("page",page);
    }

    /**
     * 人员指标成绩对比
     */
    @RequestMapping("/personalObjectScoreCompare")
    public R personalObjectScoreCompare(Integer peopleId,String objectIdList){
        if(peopleId==null || objectIdList==null){
            return R.error("参数不能为空");
        }
        List<Integer> objectList = parseStrToList(objectIdList);
        List<Map<String, Object>> page=statiService.personalObjectScoreCompare(peopleId,objectList);
        return R.ok().put("page",page);
    }

    /**
     * 单位平均成绩
     */
    @RequestMapping("/detachmentScoreCompare")
    public R detachmentScoreCompare(Integer trainingCheckId,String detachmentIdList){
        if(trainingCheckId==null||detachmentIdList==null ){
            return R.error("参数不能为空");
        }
        List<Integer> detachmentlist=parseStrToList(detachmentIdList);
        List<Map<String, Object>> page=statiService.detachmentScoreCompare(trainingCheckId,detachmentlist);
        return R.ok().put("page",page);
    }

    /**
     * 单位指标成绩对比分析
     */
    @RequestMapping("/detachmentObjectScoreCompare")
    public R detachmentObjectScoreCompare(String detachmentIdList,String objectIdList){
        if(detachmentIdList==null||objectIdList==null ){
            return R.error("参数不能为空");
        }
        List<Integer> detachmentlist=parseStrToList(detachmentIdList);
        List<Integer> objectList = parseStrToList(objectIdList);
        List<Map<String, Object>> page=statiService.detachmentObjectScoreCompare(detachmentlist,objectList);
        return R.ok().put("page",page);
    }

    /**
     * 单位指标成绩对比分析
     */
    @RequestMapping("/selectPeopleByCheckId")
    public R selectPeopleByCheckId(Integer trainingCheckId){
        if(trainingCheckId==null){
            return R.error("参数不能为空");
        }
        List<Map<String, Object>> page=statiService.selectPeopleByCheckId(trainingCheckId);
        return R.ok().put("page",page);
    }
    /**
     * 单位指标成绩对比分析
     */
    @RequestMapping("/selectDetachmentByCheckId")
    public R selectDetachmentByCheckId(Integer trainingCheckId){
        if(trainingCheckId==null){
            return R.error("参数不能为空");
        }
        List<Map<String, Object>> page=statiService.selectDetachmentByCheckId(trainingCheckId);
        return R.ok().put("page",page);
    }

    public List<Integer> parseStrToList(String arrStr){
        if(StringUtils.isEmpty(arrStr)){
            return new ArrayList<Integer>();
        }
        List<String> idsStringList = Arrays.asList(arrStr.split(","));
        List<Integer> list = new ArrayList<>();
        for (String item:idsStringList) {
            Integer objectId=Integer.valueOf(item);
            list.add(objectId);
        }
        return list;
    }



}
